--[[
  杭州银行 - 统一服务器
]]
require("apm_core")
local ApmXml = require("ApmXml")
local ApmJson = require("ApmJson")
require("extension")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local t_xml_tree
local t_json_tree
local s_transtype
local s_transtype_name
local s_channel
local s_channel_name
local s_flowno
local s_rspcode
local s_rspmsg

local dict_transtype = {}
local dict_channel = {}

local function detect_req(s_payload)
    if s_payload == nil then return false end
    t_xml_tree = ApmXml.load(s_payload, "UTF-8")
    return ApmXml.getValue(t_xml_tree, "/root") ~= nil
end

local function detect_resp(s_payload)
    if s_payload == nil then return false end
    t_json_tree = ApmJson.decode(s_payload)
    return t_json_tree ~= nil
end

--- 解析请求
-- return
-- true: 解析成功; false: 解析失败
local function parse_req()
    -- 交易渠道
    s_channel = ApmXml.getValue(t_xml_tree, "/root/reqChannelId/text()")
    if s_channel ~= nil then
        s_channel = string.trim(s_channel)
        if string.len(s_channel) > 2 then
            logWarn("channel too long:[%s]", s_channel)
            s_channel = string.sub(s_channel, -2)
        end
        s_channel_name = dict_channel[s_channel]
        if s_channel_name == nil then s_channel_name = s_channel end
    end
    return true
end

--- 解析响应
-- return
-- true: 解析成功; false: 解析失败
local function parse_resp()
    -- 交易类型
    s_transtype = t_json_tree.repServiceId
    if s_transtype ~= nil then
        s_transtype = string.trim(s_transtype)
        s_transtype_name = dict_transtype[s_transtype]
        if s_transtype_name == nil then s_transtype_name = s_transtype end
    end
    -- 交易流水号
    s_flowno = t_json_tree.repFlowNo
    -- 交易返回码
    s_rspcode = t_json_tree.ec
    -- 交易返回信息
    s_rspmsg = t_json_tree.em
    return true
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_req
    local s_resp

    s_req = ApmData.getField("request.http.body.1", 1)
    if s_req ~= nil then
        s_req = string.trim(s_req)
        if detect_req(s_req) then
            parse_req()
        end
    end

    s_resp = ApmData.getField("response.http.body.1", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        if detect_resp(s_resp) then
            parse_resp()
        end
    end

    if s_transtype == nil then
        logInfo("s_transtype is nil REQ:["..KRED.."%s"..RESET.."]", s_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:["..KBLUE.."%s"..RESET.."]", s_resp)
    else
        ApmData.replaceField("transaction.type_code", s_transtype)
        ApmData.replaceField("transaction.type_name", s_transtype_name)

        if s_channel ~= nil then
            ApmData.replaceField("transaction.channel_code", s_channel)
            ApmData.replaceField("transaction.channel_name", s_channel_name)
        end

        if s_flowno ~= nil then
            ApmData.replaceField("transaction.code", s_flowno)
        end

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        -- 为了降低ES的压力，返回码正常的情况下删除body内容
--        if s_rspcode ~= "0" and s_rspcode ~= "00" and s_rspcode ~= "000000" then
--            ApmData.delFields("request.http.body.1")
--            ApmData.delFields("response.http.body.1")
--        end

        logInfo("统一APP(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_transtype_name,
            s_channel_name,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--统一APP
--192.168.67.166:9183
--192.168.67.167:9083
--192.168.67.168:9083
--192.168.67.169:9083
--192.168.67.170:9083
--192.168.67.166:9188
--192.168.67.167:9188
--192.168.67.168:9188
--192.168.67.169:9188
--192.168.67.170:9188
--v1.0.5
